package com.usian.user.service.impl;/*
 *文件名：ApUserRealnameServiceImpl
 *创建者：宋璟
 *创建时间:2023/2/7 19:12
 *描述: TODO
 */

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.admin.AdminConstans;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedHashMap;

@Service
public class ApUserRealnameServiceImpl extends BaseServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        if (dto == null)return errorParam;
        return getPage(dto,Wrappers.<ApUserRealname>lambdaQuery().eq(dto.getStatus() != null ,ApUserRealname::getStatus,dto.getStatus()));
    }

    //认证表ApUserRealnameController  自媒体人表wemedia  作者表article  用户表user
    //认证表 用户表 做状态更新
    //自媒体人表  作者表  做数据插入
    @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto dto, Short status) {
        if (dto == null || dto.getId() == null) return errorParam;
        //getById查到认证的所有信息
        ApUserRealname attestation = getById(dto.getId());
        //  根据认证表的用户id查到用户表
        ApUser user_c = apUserMapper.selectById(attestation.getUserId());


        //不管成功还是失败都要更新   new一个认证表的对象  去修改
        ApUserRealname realname = new ApUserRealname();
        realname.setStatus(status);
        //找状态 不能直接就找打他，先找到id
        realname.setId(dto.getId());
        //如果失败了，给他一个是吧原因
        if (StringUtils.isNotEmpty(dto.getMsg()))realname.setReason(dto.getMsg());
        //更新的过程  把他找出来--》把他局部给改了--》在给他存进去
        updateById(realname);

        //如果状态是9的话就更新他
        if (status == AdminConstans.PASS_AUTH){
            //先去查他的名字
            WmUser wmUser = wemediaFeign.findByName(user_c.getName());
            if (wmUser == null){
                //首先更新自媒体人
                wmUser = new WmUser();
                wmUser.setApUserId(attestation.getUserId());
                //把字段都给同步过去了
                BeanUtils.copyProperties(user_c,wmUser);
                wmUser.setCreatedTime(new Date());
                wmUser.setStatus(9);
                //把他保存进去
                ResponseResult save = wemediaFeign.save(wmUser);
                LinkedHashMap map = (LinkedHashMap) save.getData();
                wmUser.setId((Integer)map.get("id"));
            }

            //作者表
            ApAuthor apAuthor = articleFeign.findByUserId(user_c.getId());
            if (apAuthor == null){
                apAuthor = new ApAuthor();
                apAuthor.setName(user_c.getName());
                apAuthor.setUserId(user_c.getId());
                apAuthor.setCreatedTime(new Date());
                apAuthor.setWmUserId(wmUser.getId());
                apAuthor.setType(2);
                //传不过去 id 所以传过来一dto把id存进去
                ResponseResult res = articleFeign.save(apAuthor);
                //是object类型 用LinkedHashMap
                LinkedHashMap map2 = (LinkedHashMap) res.getData();
                //强转成integer
                wmUser.setApAuthorId((Integer) map2.get("id"));
                wemediaFeign.updateAuthorId(wmUser);
            }

            //给falg临时设一个值
            ApUser tempUser = new ApUser();
            tempUser.setId(user_c.getId());
            tempUser.setFlag((short) 1);
            apUserMapper.updateById(tempUser);




        }
        return success;


    }













































}
